﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geometry;

namespace Bes.Swsp.ToolBox
{
  public class Conveyance : Element
  {
    private NodePoint _upNode;
    private NodePoint _downNode;

    public Conveyance()
      : base()
    {
    }

    public Conveyance(int id)
      : base(id)
    {
    }

    /// <summary>
    /// The path of the conveyance between the upstream and downstream nodes.
    /// </summary>
    public IPolyline Line { get; set; }

    /// <summary>
    /// The node at the upstream end of this conveyance.
    /// </summary>
    public NodePoint UpstreamNode
    {
      get
      {
        return _upNode;
      }

      set
      {
        if (_upNode != null)
        {
          _upNode.DownstreamConveyance = null;
        }

        _upNode = value;

        if (_upNode != null)
        {
          _upNode.DownstreamConveyance = this;
          this.Network = _upNode.Network;
        }
      }
    }

    /// <summary>
    /// The node at the downstream end of this conveyance.
    /// </summary>
    public NodePoint DownstreamNode
    {
      get
      {
        return _downNode;
      }

      set
      {
        if (_downNode != null)
        {
          _downNode.UpstreamConveyances.Remove(this);
        }

        _downNode = value;

        if (_downNode != null)
        {
          _downNode.UpstreamConveyances.Add(this);
          this.Network = _downNode.Network;
        }
      }
    }

    /// <summary>
    /// A value indicating whether this conveyance is subsurface (below ground).
    /// </summary>
    public virtual bool IsSubSurface { get; set; }

    public double MinAccumulatedFlow
    {
      get
      {
        if (UpstreamNode != null)
          return UpstreamNode.ReleasedFlow;
        else if (DownstreamNode != null)
          return DownstreamNode.TotalAccumulatedFlow;
        else
          return 0.0;
      }
    }

    public double MaxAccumulatedFlow
    {
      get
      {
        if (DownstreamNode != null)
          return DownstreamNode.TotalAccumulatedFlow;
        else if (UpstreamNode != null)
          return UpstreamNode.ReleasedFlow;
        else
          return 0.0;
      }
    }

    public double MinAccumulatedLoad
    {
      get
      {
        if (UpstreamNode != null)
          return UpstreamNode.ReleasedLoad;
        else if (DownstreamNode != null)
          return DownstreamNode.TotalAccumulatedLoad;
        else
          return 0.0;
      }
    }

    public double MaxAccumulatedLoad
    {
      get
      {
        if (DownstreamNode != null)
          return DownstreamNode.TotalAccumulatedLoad;
        else if (UpstreamNode != null)
          return UpstreamNode.ReleasedLoad;
        else
          return 0.0;
      }
    }

    public override string ToString()
    {
      return string.Format("Conveyance {0}", this.ID);
    }
  }
}
